﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NaiveBayesClassifier
{
    public class CTrainingSet
    {
        private bool verbose = false;
        protected List<IInstance> instanceSet;
        private HashSet<IConcept> conceptSet;
        private HashSet<string> attributeNameSet;

        #region Getter and Setter
        public int GetNumberOfConcepts()
        {
            return conceptSet.Count;
        }
        public int GetSize()
        {
            return instanceSet.Count;
        }
        public bool IsVerbose()
        {
            return verbose;
        }
        public void SetVerbose(bool verbose)
        {
            this.verbose = verbose;
        }
        public IInstance GetInstanceAt(int index)
        {
            return instanceSet.ElementAt(index);
        }
        public List<IInstance> GetInstanceSet()
        {
            return instanceSet;
        }
        public HashSet<IConcept> GetConceptSet()
        {
            return conceptSet;
        }
        public HashSet<string> GetAttributeNameSet()
        {
            return attributeNameSet;
        }
        #endregion

        public CTrainingSet(IInstance[] instances)
        {
            CreateTrainingSet(instances);
        }

        public CTrainingSet(string trainingFileName, char[] separator)
        {
            IInstance[] instances = CBaseInstance.LoadFromFile(trainingFileName, separator);
            CreateTrainingSet(instances);
        }

        public CTrainingSet(string[] attributeNames, string[][] data)
        {
            List<IInstance> instances = new List<IInstance>();
            for (int i = 0; i < data.Length; ++i)
            {
                int n = data[i].Length;
                string[] attributeValues = new string[n - 1];
                for (int j = 0; j < n - 1; ++j)
                    attributeValues[j] = data[i][j];
                instances.Add(CBaseInstance.CreateInstance(data[i][n - 1], attributeNames, attributeValues));
            }

            CreateTrainingSet(instances.ToArray());
        }

        private void CreateTrainingSet(IInstance[] instances)
        {
            instanceSet = new List<IInstance>();
            conceptSet = new HashSet<IConcept>();
            attributeNameSet = new HashSet<string>();

            IConcept concept;
            foreach (IInstance i in instances)
            {
                // thêm 1 bộ vào tập hợp
                instanceSet.Add(i);

                // lớp
                concept = i.GetConcept();
                if (!conceptSet.Contains(concept))
                    conceptSet.Add(concept);

                // các thuộc tính
                foreach (IAttribute a in i.GetAttributes())
                    if (a != null)
                        attributeNameSet.Add(a.GetName());
            }

            if (verbose)
            {
                Console.WriteLine("-------------------------------------------------------------");
                Console.WriteLine("Loaded " + this.GetSize() + " instances that belong into "
                    + this.GetNumberOfConcepts() + " concepts");
                Console.WriteLine("-------------------------------------------------------------");
            }
        }
    }
}
